<?php
namespace app\index\model;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 会员业务处理
 */
class Users extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$where = [];
		$where['dataFlag'] = 1;
		$user_name = input('get.user_name');
		$type = input('get.type');
		$consume_type = input('get.consume_type');
		$user_phone = input('get.user_phone');
		$classifyId = input('get.classifyId');
		$diseaseId = input('get.diseaseId');
		$staffId = input('get.staffId');
		$retirement_unit = input('get.retirement_unit');
		$bank_code = input('get.bank_code');
		$identity_card = input('get.identity_card');
		$startDate = input('startDate');
		$endDate = input('endDate');
		if($startDate!='')$where['firstTime'] = ['>=',$startDate];
		if($endDate!='')$where[' firstTime'] = ['<=',$endDate];	
		if(!empty($staffId))$where['staffId'] = $staffId;
		if(!empty($user_name))$where['user_name'] = ['like',"%$user_name%"];
		if($type != '')$where['type'] = $type;
		if($consume_type != '')$where['consume_type'] = ['like',"%$consume_type%"];
		if(!empty($user_phone))$where['user_phone'] = ['like',"%$user_phone%"];
		if($classifyId != '')$where['classifyId'] = $classifyId;
		if($diseaseId != '')$where['diseaseId'] = ['like',"%$diseaseId%"];
		if(!empty($retirement_unit))$where['retirement_unit'] = ['like',"%$retirement_unit%"];
		if(!empty($bank_code))$where['bank_code'] = ['like',"%$bank_code%"];
		if(!empty($identity_card))$where['identity_card'] = ['like',"%$identity_card%"];
		$rs = $this->where($where)->order('user_id desc')->paginate(input('pagesize/d'));

		if(!empty($rs)){
        	$classifyList = model('classify')->where(['dataFlag'=>1])->column('classifyId,classifyName');
			foreach($rs as $k=>$v){
				// if($v['consume_type']==0){
  		// 			$rs[$k]['consume_type'] = '系统性';
				// }elseif($v['consume_type']==1){
  		// 			$rs[$k]['consume_type'] = '传统型';
				// }elseif($v['consume_type']==2){
  		// 			$rs[$k]['consume_type'] = '投资型';
				// }				
			/*	if($v['total']>=10000){
  					$rs[$k]['style'] = 'A';
				}elseif($v['total']<5000){
  					$rs[$k]['style'] = 'C';
				}else{
  					$rs[$k]['style'] = 'B';
				}		*/
				if($v['type']==0){
  					$rs[$k]['type'] = '客户转介绍';
				}elseif($v['type']==1){
  					$rs[$k]['type'] = '业务扁平开发';
				}				
				$rs[$k]['diseaseName'] = join(',',model('disease')->where(['dataFlag'=>1,'diseaseId'=>['in',$v['diseaseId']]])->column('diseaseName'));
				$consumeName = [];
				if(in_array(0,explode(',',$v['consume_type'])))$consumeName[] = '系统性';
				if(in_array(1,explode(',',$v['consume_type'])))$consumeName[] = '传统型';
				if(in_array(2,explode(',',$v['consume_type'])))$consumeName[] = '投资型';
				$rs[$k]['consumeName'] = join(',',$consumeName);
				$rs[$k]['classifyName'] = isset($classifyList[$v['classifyId']]) ? $classifyList[$v['classifyId']] : '';
  				$rs[$k]['areaPathName'] = $this->areaPathName($v['areaIdPath']);
			}
		}
		return $rs;
	}
	public function areaPathName($areaIdPath){
		$areaIdPath = explode('_',$areaIdPath);
		$areaPathName = '';
		foreach($areaIdPath as $v){
			if($v)$areaPathName .= ' '.model('Areas')->getNames($v);
		}
		return $areaPathName;
	}
	public function getById($id){
		return $this->get($id);
	}
	/**
	 * 新增
	 */
	public function add(){
		$data = input('post.');
		if(!$data['staffId'])return HTRReturn('请选择服务人员');
		$data['create_time'] = time();
        //获取地区
        $areaIds = model('Areas')->getParentIs($data['areaId']);
	    if(!empty($areaIds))$data['areaIdPath'] = implode('_',$areaIds)."_";
    	HTRUnset($data,'user_id');

		if($data['user_birthday']){
			list($y1,$m1,$d1) = explode("-",$data['user_birthday']); 
			list($y2,$m2,$d2) = explode("-",date("Y-m-d")); 
			$age = $y2 - $y1; 
			if((int)($m2.$d2) < (int)($m1.$d1)) 
			$age -= 1; 
			$data['user_age'] = $age;							
		}		
    	$result = $this->allowField(true)->save($data);
    	if($result){
    		if($data['user_birthday']){
    			$md = substr($data['user_birthday'],5);
    			$data = [];
    			$y = date('Y');
    			if($md < date('m-d'))$y++;
	    		$user_id = $this->getLastInsID();
    			for($i=0;$i<20;$i++){
    				$data[] = ['user_id' => $user_id,'remindTime' => $y.'-'.$md];
    				$y++;
    			}
				model('Remind')->insertAll($data);			
    		}		
	        return HTRReturn("新增成功", 1);
    	}else{
            return HTRReturn('新增失败');
        }	
	}
    /**
	 * 编辑
	 */
	public function edit(){
		$Id = (int)input('post.user_id');
		$data = input('post.');
		if(!$data['staffId'])return HTRReturn('请选择服务人员');
		if($data['user_phone']){
            $user=$this->where('user_id',$Id)->find();
		    $u = $this->where('user_phone',$data['user_phone'])->find();
		    if($u) {
                if ($user['user_phone'] != $u['user_phone']) {
                    return HTRReturn('此手机号已注册，请更改联系方式');
                }
            }
        }
        //获取地区
        $areaIds = model('Areas')->getParentIs($data['areaId']);
	    if(!empty($areaIds))$data['areaIdPath'] = implode('_',$areaIds)."_";
		HTRUnset($data,'create_time,user_id');
		if($data['user_birthday']){
			list($y1,$m1,$d1) = explode("-",$data['user_birthday']); 
			list($y2,$m2,$d2) = explode("-",date("Y-m-d")); 
			$age = $y2 - $y1; 
			if((int)($m2.$d2) < (int)($m1.$d1)) 
			$age -= 1; 
			$data['user_age'] = $age;							
		}		
	    $user_birthday = $this->where('user_id',$Id)->value('user_birthday');	    
	    $result = $this->allowField(true)->save($data,['user_id'=>$Id]);
        if(false !== $result){        	
    		if($user_birthday != $data['user_birthday']){  		
				model('Remind')->where(['is_read'=>0,'type'=>0,'user_id'=>$Id])->delete();	 
				if($data['user_birthday']){
	    			$md = substr($data['user_birthday'],5);
	    			$data = [];
	    			$y = date('Y');
	    			if($md < date('m-d'))$y++;
	    			for($i=0;$i<20;$i++){
	    				$data[] = ['user_id' => $Id,'remindTime' => $y.'-'.$md];
	    				$y++;
	    			}
					model('Remind')->insertAll($data);	   					
				} 			
    		}		
        	return HTRReturn("编辑成功", 1);
        }else{
            return HTRReturn('编辑失败');
        }
	}
	/**
	 * 删除
	 */
	public function del(){
	    $id = input('post.id/d');
		$data = [];
		$data['dataFlag'] = -1;
	    $result = $this->update($data,['user_id'=>$id]);
        if(false !== $result){
			model('Remind')->where(['is_read'=>0,'user_id'=>$id])->delete();	
        	return HTRReturn("删除成功", 1);
        }
        return HTRReturn('删除失败');
	}
    /**
     * 判断手机号是否存在
     */
    public function checkPhone(){
    	$user_phone = input('post.user_phone');
    	$user_id = input('post.user_id/d',0);
	    if($user_phone=='')return HTRReturn("手机号码不能为空");
		$where = [];
		$where['dataFlag'] = 1;
		$where['user_phone'] = $user_phone;
	    $dbo = $this->where($where);
	    if($user_id>0){
	    	$dbo->where("user_id", "<>", $user_id);
	    }
	    $rs = $dbo->count();
	    if($rs==0){
	    	return HTRReturn("该手机号码可用",1);
	    }
	    return HTRReturn("对不起，手机号码已存在");
    }
}
